package com.zealcomm.ivcscustomer.fragment

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zco.util.ConvertUtil
import com.zco.util.ToastUtil
import com.zco.base.annotations.CommandStatus
import com.zco.base.entity.Command
import com.zco.base.entity.SessionMessage
import com.zealcomm.commonui.base.BaseFragment2
import com.zealcomm.commonui.widget.RecyclerViewDivider
import com.zco.ivcs.customer.IvcsCustomerCallback
import com.zealcomm.ivcscustomer.R
import com.zealcomm.ivcscustomer.app.IvcsApplication
import com.zealcomm.ivcscustomer.databinding.FragmentMakeChoiceBinding
import com.zealcomm.ivcscustomer.databinding.ItemChoiceBinding
import com.zealcomm.ivcscustomer.service.MainActivity
import com.zealcomm.utils.bindView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class MakeChoiceFragment(makeChoiceSessionMessage: SessionMessage<Command<MutableList<String>>>) :
    BaseFragment2(R.layout.fragment_make_choice) {

    private val binding: FragmentMakeChoiceBinding by bindView()
    private val choices = mutableListOf<String>()
    private val makeChoiceSessionMessage = makeChoiceSessionMessage

    init {
        makeChoiceSessionMessage.data.data?.let {
            this.choices.addAll(it)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.rvChoice.addItemDecoration(RecyclerViewDivider(
            requireContext(), LinearLayoutManager.VERTICAL,
            ConvertUtil.dp2px(20), Color.TRANSPARENT, true
        ))
        binding.rvChoice.adapter = object : RecyclerView.Adapter<MyViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
                return MyViewHolder(ItemChoiceBinding.inflate(layoutInflater, parent, false))
            }

            override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
                holder.binding.tvChoice.text = choices[position]
                holder.binding.tvChoice.setOnClickListener {
                    val responseCommand =
                        Command<Answer>(makeChoiceSessionMessage.data.cmd, Answer(choices[position]))
                    responseCommand.id = makeChoiceSessionMessage.data.id
                    responseCommand.status = CommandStatus.DONE
                    IvcsApplication.ivcsCustomer!!.sendPeerResponse(
                        makeChoiceSessionMessage.from,
                        responseCommand,
                        object : IvcsCustomerCallback {
                            override fun onSuccess() {
                                GlobalScope.launch(Dispatchers.Main) {
                                    (holdingActivity as MainActivity).removeFragment()
                                }
                            }

                            override fun onFailed(code: Int, error: String?) {
                                GlobalScope.launch(Dispatchers.Main) {
                                    ToastUtil.showMiddleToast(error)
                                }
                            }

                        })
                }
            }

            override fun getItemCount(): Int {
                return choices.size
            }

        }
    }

    private class MyViewHolder(binding: ItemChoiceBinding) :
        RecyclerView.ViewHolder(binding.root) {
        var binding: ItemChoiceBinding = binding
    }

    private data class Answer(val answer: String)
}